package com.feilong.edgeAI.adapter;

import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.databinding.ViewDataBinding;
import androidx.recyclerview.widget.RecyclerView;

import com.aiibt.common.core.WDRecyclerAdapter;
import com.feilong.edgeAI.R;
import com.feilong.edgeAI.databinding.RemindItemBinding;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RemindAdapter extends WDRecyclerAdapter<String> {
    @RequiresApi(api = Build.VERSION_CODES.N)
    public RemindAdapter(List<String> data) {
        List<String> list = new ArrayList<>();
        for (String datum : data) {
            if(datum.startsWith("@")){
                continue;
            }
            datum = datum.replace("=","、");
            list.add(datum);
        }
        addAll(list);
    }
    private Map<String,String> map=new HashMap<>();
    private boolean show;
    @Override
    protected int getLayoutId() {
        return R.layout.remind_item;
    }
    public void showCheck(boolean show){
        this.show = show;
        map.clear();
        notifyDataSetChanged();
    }
    public boolean getShowCheck(){
        return show;
    }
    @Override
    protected void bindView(ViewDataBinding binding, String item, int position) {
        RemindItemBinding b = (RemindItemBinding) binding;
        b.checkbox.setVisibility(show?View.VISIBLE:View.GONE);
        b.checkbox.setChecked(!TextUtils.isEmpty(map.get(item)));
        b.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    map.put(item,String.valueOf(position+1));
                }else{
                    map.remove(item);
                }
                if(null!=onItemClickListener){
                    onItemClickListener.onItemClick(map);
                }
            }
        });
        b.text.setText(item);
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public String delete() {
        StringBuilder stringBuilder = new StringBuilder();
        map.entrySet().forEach(entry -> {
            String key = entry.getKey();
            stringBuilder.append(entry.getValue());
            stringBuilder.append("-");
            remove(key);
        });
        if(null!=onItemClickListener){
            onItemClickListener.onFinish();
        }
        stringBuilder.deleteCharAt(stringBuilder.length()-1);
        notifyDataSetChanged();
        return stringBuilder.toString();
    }
    public interface OnItemClickListener<T>{
        void onItemClick(T item);
        void onFinish();
    }
    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
}
